<?php

class Core_Frontend_Component_Article_Controller_Content extends Core_Frontend_Share_Component_Controller {

    public function detailAction($id, $page = 1) {
        
        $content = App::getModel('article/content')
                        ->updateClickCount($id)
                        ->load($id);
        $themeId = $content->getThemeId();
        if (empty($themeId)) {
            $themeId = $content->getCategory()->getThemeId();
        }
        $pageTemplate = App::getModel('design/collection_page_template')
                        ->filterByPageThemeId($themeId)
                        ->filterByFileName('detail')
                        ->filterByEnabled(1)
                        ->load()
                        ->getFirstItem();
        $this->getVariables()->setData('page_template', $pageTemplate);
        $this->getVariables()->setData('article_object', $content);
        $this->getVariables()->setData('comment_article_id', $id);
        Kd_Web::getPage()->getNavigation()->addLink('新闻列表', $this->getUrl(NULL,'conlist', 'content'))
                ->addLabel($content->getTitle());
        Kd_Web::getPage()->setTitle($content->getTitle())
                ->setKeyword($content->getTag())
                ->addMeta('name', 'description', $content->getIntroduction());
        return $this->pageResult($pageTemplate);
    }

    public function conlistAction($id = NULL, $page = 1) {
        $themeId = '61c35635157c11e08977dc18e12206b6';
        if ($id) {
            $themeId = App::getModel('article/category')->load($id)->getThemeId();
        }
        $pageTemplate = App::getModel('design/collection_page_template')
                        ->filterByPageThemeId($themeId)
                        ->filterByFileName('conlist')
                        ->filterByEnabled(1)
                        ->load()
                        ->getFirstItem();
        if($page == 1){
            $startIndex = 0;
            $pageSize = 13;
        }
        else if($page>1){
            $startIndex = 13+($page-2)*20;
            $pageSize = 20;
        }
        $article_list = App::getModel('article/collection_content')
                        ->filterByStatus('published')
                        ->limit($pageSize,$startIndex);
        
        if (!is_null($id)) {
            $article_list->filterByCategoryId($id);
        }
        $article_list->load();

        $records = $article_list->loadCount();
        $args = array_merge($this->getRequest()->getAllParameters(), array('page' => '{index}'));
        $pagination = new Kd_Html_Pagination();
        $pagination->setPageIndex($page)
                   ->setPageSize($pageSize)
                   ->setPageRange(6)
                   ->setRecordCount($records)
                   ->setUrlFormat($this->getUrl($args));
        Kd_Web::getPage()->getNavigation()->addLabel('文章列表');
        Kd_Web::getPage()->setTitle('列表页')->setKeyword('');
        $this->getVariables()
                ->setData('article_list', $article_list)
                ->setData('article_list_count', $records)
                ->setData('article_list_page_index', $page)
                ->setData('article_list_page_size', 20)
                ->setData('article_list_pagination', $pagination)
                ->setData('page_template', $pageTemplate);
               
        return $this->pageResult($pageTemplate);
    }

    

//    public function previewAction($id, $page = 1) {
//        if ($this->getSession()->has('article_preview')) {
//            $content = App::getModel('article/content')
//                            ->updateClickCount($id)
//                            ->load($id);
//            $this->getSession()->remove('article_preview');
//            return $this->viewResult(array('content' => $content, 'pageindex' => $page));
//        }
//        die('非法操作');
//    }

    public function getContentAction(){
        $content = App::getModel('article/content')->load(27);
        echo $content->getId();
    }

    public function previewAction($id){
        if($this->getSession()->has('is_article_preview')){
            $this->getSession()->remove('is_article_preview');
            return $this->redirectResult($this->getUrl(array('id'=>$id),'detail','content','article'));
        }
        die('非法操作');
    }

}